/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui;

import com.toedter.calendar.JDateChooser;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Creates task display elements. It provides elements not only to display,
 * but also to edit tasks, this components is the base to the compoent
 * used as the renderer and editor
 * to the first column of the table view, and as the rendered object of
 * the list view
 * 
 * @author Nosheen Zaza
 * @author Manuel Rebollo
 * @author Wo Ke
 */
public class TaskDisplay extends JPanel {

    protected CheckTextField checkTextField;
    protected ProgressMeter progressMeter;

    /**
     * Shows the progress in the view mode.
     */
    protected JLabel progressLabel;

    /**
     * Shows the date in the view mode.
     */
    protected JLabel dateLabel;

    protected JButton deleteButton;
    
    /**
     * This variable specifies whether the task display is in view or
     * edit mode.
     */
    protected boolean compactMode;

    /**
     * Contains elements to be shown in the view mode, other than the
     * task status and text
     */
    protected JPanel detailsPanel;

    /**
     * Contains elements to be shown in the edit mode, to edit progress,
     * date, and allow deleting a task.
     */
    protected JPanel morePanel;

    protected JDateChooser dateChooser;
    protected ResourceBundle settings;

    /**
     * Creates and initializes a new task display component.
     */
    public TaskDisplay() {
        settings = ActionControl.getInstance().getStringsBundle();
        compactMode = true;
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        checkTextField = new CheckTextField();
        /*
         * Strike done tasks only.
         */
        checkTextField.getCheckBox().addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
               if(checkTextField.getCheckBox().isSelected()) {
                   checkTextField.getTextfield().setFont(
                           UIManager.getDefaults().getFont(
                           "Font.Dialog-Plain-Striked-12"));
               }
               else if(!checkTextField.getCheckBox().isSelected()) {
                   checkTextField.getTextfield().setFont(
                           UIManager.getDefaults().getFont(
                           "Font.Dialog-Plain-12"));
               }
            }
        });
        
        /*
         * Setup the progress meter.
         */
        progressMeter = new ProgressMeter();
        progressMeter.getProgressBar().setMaximumSize(new Dimension(80, 30));
        progressMeter.getProgressBar().setPreferredSize(new Dimension(80, 30));
        progressMeter.setMaximumSize(new Dimension(100, 30));
        progressMeter.setOpaque(false);
        setProgressButtons(progressMeter.getIncreaseButton());
        setProgressButtons(progressMeter.getDecreaseButton());
        progressMeter.getProgressBar().setOpaque(false);
        progressMeter.getProgressBar().setFont(
                UIManager.getDefaults().getFont("Font.Dialog-Bold-10"));
        progressMeter.getProgressBar().setStringPainted(true);
        progressMeter.setProgress(0);

        progressLabel = new JLabel();
        progressLabel.setLabelFor(null);
        progressLabel.setFont(
                UIManager.getDefaults().getFont("Font.Dialog-Plain-10"));
        progressLabel.setForeground(
                UIManager.getDefaults().getColor("Color.Gray"));
        progressLabel.setOpaque(false);

        dateLabel = new JLabel();
        dateLabel.setFont(
                UIManager.getDefaults().getFont("Font.Dialog-Plain-10"));
        dateLabel.setForeground(
                UIManager.getDefaults().getColor("Color.Gray"));
        progressLabel.setText(settings.getString("Progress") + ":" + (0) + "%");
        
        detailsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        detailsPanel.setOpaque(false);
        detailsPanel.add(dateLabel);
        detailsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        detailsPanel.add(progressLabel);

        dateChooser = new JDateChooser();
        dateChooser.setDateFormatString("dd-MM-yyyy");
        dateChooser.setMaximumSize(new Dimension(150, 30));
        dateChooser.setPreferredSize(new Dimension(150, 30));
        
        deleteButton = new JButton();
        deleteButton.setContentAreaFilled(false);
        deleteButton.setBorder(null);

        morePanel = new JPanel();
        morePanel.setLayout(new BoxLayout(morePanel, BoxLayout.LINE_AXIS));
        morePanel.add(dateChooser);
        morePanel.add(progressMeter);
        morePanel.add(deleteButton);
        morePanel.setOpaque(false);
        morePanel.setVisible(false);

        morePanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 10, 5));

        setOpaque(false);
        add(checkTextField);
        add(detailsPanel);
        add(morePanel);
        
    }

    /**
     * Returns the check text field of this component.
     * @return checkTextField
     */
    public CheckTextField getCheckTextField() {
        return checkTextField;
    }

    /**
     * Sets the {@code CheckTextField} of this instance.
     * @param checkTextField
     */
    public void setCheckTextField(CheckTextField checkTextField) {
        this.checkTextField = checkTextField;
    }

    /**
     * Returns the progress meter of this instance
     * @return progressMeter
     */
    public ProgressMeter getProgressMeter() {
        return progressMeter;
    }

    /**
     * Sets the progress meter of this instance
     * @param progressMeter
     */
    public void setProgressMeter(ProgressMeter progressMeter) {
        this.progressMeter = progressMeter;
    }

    /**
     * Sets up the buttons of this task display.
     * @param button
     */
    private void setProgressButtons(JButton button) {
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);
        button.setFont(UIManager.getDefaults().getFont("Font.Dialog-Plain-12"));
        button.setOpaque(false);

    }
    
    /**
     * Returns the date chooser component.
     * @return dateChooser
     */
    public JDateChooser getDateChooser() {
        return dateChooser;
    }

    /**
     * Returns the delete button component
     * @return deleteButton
     */
    public JButton getDeleteButton() {
        return deleteButton;
    }
}
